/*
 *	Copyright (C) 2008 - Mauricio Bieze Stefani
 *	This file is part of the Insano Game Engine.
 *
 *	Insano is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Insano is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with Insano.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef GAMECLIENT_H_INCLUDED
#define GAMECLIENT_H_INCLUDED

#include <irrlicht.h>
#include "MBSSlot.h"
#include "Tempo.h"
#include "ClientConfig.h"
#include "InputDevices.h"
#include "MySelf.h"
#include "MapManager.h"
#include "GameUI.h"
#include "EntityFactory.h"

class GameClient :  public irr::IEventReceiver
{
public:
	GameClient();
	~GameClient();

    // Irrlicht
	irr::IrrlichtDevice *device;
	irr::video::IVideoDriver *driver;
	irr::scene::ISceneManager *smgr;

	// GUI
	GUIManager gui;
	virtual bool OnEvent(const irr::SEvent& event);

	// windows
	irr::gui::IGUIStaticText *info;

	// general configuration
	ClientConfig config;

    // network
    enum eProtocolState
    {
    	DISCONNECTED,	//
		IDLE,			//
		WAIT_LOGIN,		// waiting for LOGIN_REQ
		WAIT_NEW_ACC,	// waiting for CREATE_ACC_REQ
		WAIT_CHAR_LIST,	// waiting for CHAR_LST_REQ
		WAIT_CHAR_NEW,	// waiting for CREATE_CHAR_REQ
		WAIT_CHAR_LOGIN	// waiting for LOGIN_CHAR_REQ
    } protocolState;

    MBSSlot slot;
    MBSClientSocketSelector sockSel;
    int login_timeout;
    int conectar();
    int desconectar();
    int handleNet();
   	int sendChat(const std::string& dest, const std::string& msg);
	int sendLogin();
	int sendNewAccount();
	int sendGetCharList();
	int sendLoginChar(unsigned long id);
	int sendCreateChar(const char *newChar, unsigned char race, unsigned char gender);

	// input devices
    Keyboard kbd;
    Mouse mouse;

	// main loop
	int run();

	// various
	Tempo tempo;
	void takeScreenshot();
	enum GameState
	{
		FATAL_ERROR,
		WELCOME_SCREEN,
		LOGIN_SCREEN,
		PROGRESS_SCREEN,
		CHAR_SCREEN,
		PLAYING
	} gameState;
	GameState changeGameState(GameState newstate);
	MySelf me;	// player
	std::string currentMap;
	int setMap(const std::string& mapname);
	MapManager mapManager;
	EntityFactory eFac;
	Map *world;
private:
	ProtoCharList auxCharList;
};

#endif // GAMECLIENT_H_INCLUDED
